package cn.swu.lzr.collection;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;

public class AddToCollection extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.valueOf(request.getParameter("eventId"));

        HttpSession session = request.getSession();
        if ((ArrayList<Integer>)session.getAttribute("Collection") == null){
            ArrayList<Object> ItemList = new ArrayList<>();
            ItemList.add(id);
            session.setAttribute("Collection",ItemList);
        }else{
            ArrayList<Integer> ItemList = (ArrayList<Integer>) session.getAttribute("Collection");
            if (!ItemList.contains(id))
            {
                ItemList.add(id);
                session.setAttribute("Collection",ItemList);
            }
        }

        response.sendRedirect("/java-design/index.html");
    }


}
